import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.args.ListPosition;

import java.util.List;

public class RedisString_List {
    public static void testMSetAndMGet(Jedis jedis){
        jedis.mset("key1","LH","key2","wyf","key3","meme");
        List<String> mget = jedis.mget("key1", "key2", "key3");
        System.out.println(mget);
    }
    public static void testAppend(Jedis jedis){
        jedis.append("key1","aaa");
        String key1 = jedis.get("key1");
        System.out.println(key1);
        jedis.append("key1","bbb");
        key1=jedis.get("key1");
        System.out.println(key1);
    }
    private static void testGetRangeAndSetRange(Jedis jedis) {
        jedis.set("key1","abcd");
        String key1 = jedis.getrange("key1", 1, 3);
        System.out.println(key1);
//        从 offset 索引开始，用 value 的字符依次覆盖原字符串对应位置的字符，若 value 长度超过原字符串剩余长度，则直接追加
         jedis.setrange("key1", 0, "Maybe");
         key1=jedis.get("key1");
        System.out.println(key1);
    }
    private static void testSetnx(Jedis jedis){
        //不存在 就成功
        long setnx = jedis.setnx("key", "MM");
        System.out.println(setnx);
        String key = jedis.get("key");
        System.out.println(key);
        //存在 失败
        setnx=jedis.setnx("key","02");
        System.out.println(setnx);
        key=jedis.get("key");
        System.out.println(key);

    }
    private static void testPsetexAndPttl(Jedis jedis){
        //不存在 才成功设置
//        设置 key 并指定过期时间（毫秒），如果 key 已存在，会覆盖原有的值和过期时间（与 setex 类似，只是单位不同）
        jedis.psetex("key",5000,"02");
        long key = jedis.pttl("key");
        System.out.println(key);
    }
    private static void testIncrAndDecr(Jedis jedis){
        jedis.setnx("key","0");
        jedis.incr("key");
        System.out.println(jedis.get("key"));
        jedis.decr("key");
        System.out.println(jedis.get("key"));
    }
    private static void testIncrByAndDecrBy(Jedis jedis){
        jedis.set("key","0");
        jedis.incrBy("key",10);
        System.out.println(jedis.get("key"));
        jedis.decrBy("key",5);
        System.out.println(jedis.get("key"));
    }
    private static void testLpushAndLpop(Jedis jedis){
        //头插
        long n=jedis.lpush("key","1","2","3","4","5");
        System.out.println(n);

        //头删
        String key = jedis.lpop("key");
        System.out.println(key);
        key=jedis.lpop("key");
        System.out.println(key);
        key=jedis.lpop("key");
        System.out.println(key);
        key=jedis.lpop("key");
        System.out.println(key);
        key=jedis.lpop("key");
        System.out.println(key);

    }
    private static void testRpushAndRpop(Jedis jedis){
        //尾插
        long n=jedis.rpush("key","1","2","3","4","5");
        System.out.println(n);

        //尾删
        String key = jedis.rpop("key");
        System.out.println(key);
        key=jedis.rpop("key");
        System.out.println(key);
        key=jedis.rpop("key");
        System.out.println(key);
        key=jedis.rpop("key");
        System.out.println(key);
        key=jedis.rpop("key");
        System.out.println(key);

    }
    private static void testLrange(Jedis jedis){
        //头插
        jedis.rpush("key","1","2","3","4");
        List<String> key = jedis.lrange("key", 1, 3);
        System.out.println(key);
    }
    private static void testBLpop(Jedis jedis){
        while(true){
            //头删
            //超时时间设为0表示死等
            List<String> key = jedis.blpop(0, "key");
            System.out.println(key);
        }

    }
    private static void testBRpop(Jedis jedis){
        //尾删
        System.out.println("开始调用brpop");
        while(true){
            List<String> key = jedis.brpop(0, "key");
            System.out.println(key);
        }
    }
    private static void testLindex(Jedis jedis){
        jedis.rpush("key","1","2","3","4");
        //返回对应下标的值
        String key = jedis.lindex("key", 2);
        System.out.println(key);
    }
    private static void testLinsert(Jedis jedis){
        jedis.rpush("key","a","b","c","d");
        //在c的前面加上100
        jedis.linsert("key", ListPosition.BEFORE,"c","100");
        List<String> key = jedis.lrange("key", 0, -1);
        System.out.println(key);

    }
    private static void testLen(Jedis jedis){
        jedis.rpush("key","a","b","c");
        long key = jedis.llen("key");
        System.out.println(key);

    }



    public static void main(String[] args) {
        JedisPool jedisPool=new JedisPool("tcp://127.0.0.1:8888");
        try(Jedis jedis=jedisPool.getResource()){
            testMSetAndMGet(jedis);
//            testAppend(jedis);
//            testGetRangeAndSetRange(jedis);
//            testSetnx(jedis);
//            testPsetexAndPttl(jedis);
//            testIncrAndDecr(jedis);
//            testIncrByAndDecrBy(jedis);
//            testLpushAndLpop(jedis);
//            testRpushAndRpop(jedis);
//            testLrange(jedis);
//            testBLpop(jedis);
//            testBRpop(jedis);
//            testLindex(jedis);
//            testLinsert(jedis);
            testLen(jedis);


        }
    }
}
